<?php
/**
 * Created by PhpStorm.
 * User: y3731
 * Date: 2018/12/13
 * Time: 16:21
 */

namespace app\common\library\token\driver;
use app\common\library\token\Driver;
use think\Cache;

class File extends Driver
{

    protected $options = [
       'type' => 'File',
        // 缓存有效期为永久有效
        'expire'=>  3600,
        //缓存前缀
        'prefix'=>  'think',
        'path'  =>  'runtime/cache/',
    ];

    /**
     * 构造函数
     * @param array $options 缓存参数
     *
     */
  public function __construct($options = [])
  {
        if (!empty($options)){
            $this->options = array_merge($this->options,$options);
        }

        $this->handler = Cache::connect($options);
  }

    /**
     * 存储Token
     * @param   string $token Token
     * @param   int $id  ID
     * @param   int $expire 过期时长,0表示无限,单位秒
     * @return bool
     */
    public function set($token, $id, $expire = 0)
    {
        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }

       $this->handler->set($token,$id,$expire);

        return true;
    }

    /**
     * 获取Token内的信息
     * @param   string $token
     * @return  array
     */
    public function get($token)
    {
        $result = $this->handler->get($token);

        return $result;
    }

    /**
     * 判断Token是否可用
     * @param   string $token Token
     * @param   int $idD
     * @return  boolean
     */
    public function check($token, $id)
    {
        $data = self::get($token);

        return $data == $id ? true : false;
    }

    /**
     * 删除Token
     * @param   string $token
     * @return  boolean
     */
    public function delete($token)
    {
         $data = self::get($token);

         if ($data){
             $this->handler->clear();
         }

         return true;
    }

    /**
     * 删除指定id的所有Token
     * @param   int $id
     * @return  boolean
     */
    public function clear($id){

    }
}